// pages/run/run.js
import WxValidate from "../../utils/WxValidate";
import showValidateModal from "../../utils/showValidateModal";
import utils from "../../utils/util";
const app = getApp();
Page({
  handleFooter() {
    wx.showModal({
      cancelColor: "cancelColor",
      title: "用户需知",
      content: "租借时长由租借者自行估算，决定最终金额！",
      showCancel: false,
    });
  },
  //选择快递商家
  businessChange(e) {
    this.setData({
      bussinessIndex: e.detail.value,
      "form.rentDuration": this.data.bussinessArray[e.detail.value],
    });
  },
  /* 跳转地址 */
  selectAddress() {
    wx.navigateTo({
      url: "../addres/addres",
    });
  },
  showTimeChange(e) {
    this.setData({
      showTimeIndex: e.detail.value,
      "form.deliveryTime": this.data.showTimeArray[e.detail.value],
    });
  },
  /**
   * 页面的初始数据
   */
  data: {
    bussinessArray: [
      "30分钟归还",
      "1小时归还",
      "3小时归还",
      "12小时归还",
      "24小时归还",
      "超过一天，自行私信聊谈",
    ],
    //快递商家数组
    showTimeArray: [
      "租借后的时长在延迟最多十分钟",
      "租借后的时长在延迟最多十五分钟",
      "租借后的时长在延迟最多二十分钟",
      "租借后的时长在延迟最多三十分钟",
      "租借后的时长在延迟最多一个小时",
    ],
    showTimeIndex: 0,
    bussinessIndex: 0, //选中得快递商家数组下标
    form: {
      content: "", //租借内容
      address: "", //收货地点
      payMoney: 3, //支付金额
      rentDuration: "30分钟归还", //租借时长
      deliveryTime: "租借后的时长在延迟最多十分钟", //预计交货时间
    },
    baseMoney: 3, //跑腿费
  },
  //计算金额
  countMoney(e) {
    //获取输入金额
    let money = e.detail.value;
    //判断是否为数字
    if (isNaN(money)) {
      wx.showToast({
        title: "请输入正确金额。",
        icon: "error"
      });
      return false;
    }
    if (money.trim() != "") {
      money = Number(money) + Number(this.data.baseMoney);
      this.setData({
        "form.payMoney": money
      });
    } else {
      this.setData({
        "form.payMoney": 3
      });
    }
  },
  //帮助内容 取货地点
  timeID: 0, //定时器ID
  handleInput(e) {
    const {
      name
    } = e.currentTarget.dataset;
    const {
      value
    } = e.detail;
    clearTimeout(this.timeID); //清楚timeID对应的定时器，即取消相应函数的执行
    this.timeID = setTimeout(() => {
      switch (name) {
        case "content":
          this.setData({
            "form.content": value
          });
          break;
      }
    }, 500);
  },
  //表单保存
  async submitForm() {
    //判断当前用户是否已授权登录
    const r = await showValidateModal.cklogin(app.globalData.userInfo);
    if (!r) {
      return false;
    }
    const {
      content,
      address,
      payMoney,
      rentDuration,
      deliveryTime,
    } = this.data.form;
    const params = {
      content,
      address
    }; //检测表单数据
    if (!this.WxValidate.checkForm(params)) {
      const error = this.WxValidate.errorList[0];
      showValidateModal.showValidateModal(error);
      return false;
    }
    wx.request({
      url: `${app.globalData.server}/addOrder`,
      method: "POST",
      data: {
        openid: app.globalData.userInfo.openid,
        orderType: "租借服务",
        endAddress: address, //目的地
        payMoney,
        publicTime: utils.formatTime(new Date()),
        status: "待帮助",
        info: {
          content,
          rentDuration,
          deliveryTime
        },
        userInfo: app.globalData.userInfo,
      },
      success: (res) => {
        wx.hideLoading({});
        if (res.data == "success") {
          wx.showToast({
            title: "提交成功",
            icon: "success"
          });
          setTimeout(() => {
            wx.switchTab({
              url: "../order/order"
            });
          }, 1000);
        } else {
          wx.hideLoading({});
          wx.showToast({
            title: "发布失败",
            icon: "error"
          });
        }
      },
      fail: (err) => {
        wx.hideLoading({});
        wx.showToast({
          title: "发布失败",
          icon: "error"
        });
      },
    });
  },
  /**
   * 生命周期函数--监听页面加载
   */
  onLoad(options) {
    this.initValidate();
  },
  initValidate() {
    // 验证字段的规则
    const rules = {
      content: {
        required: true,
        maxlength: 30
      },
      address: {
        required: true
      },
    };
    // 验证字段的提示信息，若不传则调用默认的信息
    const messages = {
      content: {
        required: "请输入租借信息",
        maxlength: "租借信息不能超过30个字符",
      },
      address: {
        required: "请选择收货地点"
      },
    };
    // 创建实例对象
    this.WxValidate = new WxValidate(rules, messages);
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady() {},

  /**
   * 生命周期函数--监听页面显示
   */
  onShow() {},

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide() {},

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload() {},

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh() {},

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom() {},

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage() {},
});